// pages/level/level.js
var util = require("../../utils/util.js");
Page({
    //分享
    onShareAppMessage: function () { },
    /**
     * 页面的初始数据
     */
    data: {
        level1: [
            { name: "V1勋章", type: 1, url: "../../images/xunzhang/rank1.png", active: "../../images/xunzhang/rank11.png", checked: false , day: 7},
            { name: "V2勋章", type: 2, url: "../../images/xunzhang/rank2.png", active: "../../images/xunzhang/rank22.png", checked: false, day: 21},
            { name: "V3勋章", type: 3, url: "../../images/xunzhang/rank3.png", active: "../../images/xunzhang/rank33.png", checked: false, day: 45},
            { name: "V4勋章", type: 4, url: "../../images/xunzhang/rank4.png", active: "../../images/xunzhang/rank44.png", checked: false, day: 69},
            { name: "V5勋章", type: 5, url: "../../images/xunzhang/rank5.png", active: "../../images/xunzhang/rank55.png", checked: false, day: 98},
            { name: "V6勋章", type: 6, url: "../../images/xunzhang/rank6.png", active: "../../images/xunzhang/rank66.png", checked: false, day: 121},
            { name: "V7勋章", type: 7, url: "../../images/xunzhang/rank7.png", active: "../../images/xunzhang/rank77.png", checked: false, day: 145},
            { name: "V8勋章", type: 8, url: "../../images/xunzhang/rank8.png", active: "../../images/xunzhang/rank88.png", checked: false, day: 196},
            { name: "V9勋章", type: 9, url: "../../images/xunzhang/rank9.png", active: "../../images/xunzhang/rank99.png", checked: false, day: 200},
        ],
        level2: [
            { name: "阅读新星", type: 11, url: "../../images/xunzhang/xz1.png", active: "../../images/xunzhang/xz11.png", checked: false, day: 21},
            { name: "阅读达人", type: 12, url: "../../images/xunzhang/xz2.png", active: "../../images/xunzhang/xz22.png", checked: false, day: 45},
            { name: "运动新星", type: 13, url: "../../images/xunzhang/xz3.png", active: "../../images/xunzhang/xz33.png", checked: false, day: 21},
            { name: "运动达人", type: 14, url: "../../images/xunzhang/xz4.png", active: "../../images/xunzhang/xz44.png", checked: false, day: 45},
            { name: "美食新星", type: 15, url: "../../images/xunzhang/xz5.png", active: "../../images/xunzhang/xz55.png", checked: false, day: 21},
            { name: "美食达人", type: 16, url: "../../images/xunzhang/xz6.png", active: "../../images/xunzhang/xz66.png", checked: false, day: 45},
        ],
        show_mode: false,
        xunzhang: "",
        xz_name: "",
        total: 0,//获取该勋章的总人数
        //展示面板参数
        showActive:false,
        userInfo:"",
        xunzhangName:"",
        showImg:"",
        xz_day:""
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.getDetail();
        var userInfo = wx.getStorageSync("userInfo");
        if(!!(userInfo) && userInfo != ""){
            userInfo = JSON.parse(userInfo);
            this.setData({
                userInfo:userInfo
            });
        }
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },
    show_xz(options) {
        var type = options.currentTarget.dataset.type;
        var level1 = this.data.level1;
        var level2 = this.data.level2;
        //获取某个勋章信息
        var temp = {};
        if(type*1 < 10){
            temp = level1[type*1-1];
        }else{
            temp = level2[type*1-11];
        }
        this.setData({
            show_mode: true,
            showActive:temp.checked,
            xunzhangName:temp.name,
            showImg: temp.checked?temp.active:temp.url,
            xz_day:temp.day
        })
        this.getTotal(type);
    },
    hide_xz() {
        this.setData({
            show_mode: false
        })
    },
    //获取我的勋章信息
    getDetail: function () {
        var that = this;
        var userInfo = JSON.parse(wx.getStorageSync("userInfo"));
        var level1 = this.data.level1;
        var level2 = this.data.level2;
        util.wxAjax("admin/myDecorated", "post", {
            adminid: userInfo.smAdmin.id
        }, function (res) {
            var data = res.data;
            if(!!data.data){
                var jsonData = data.data;
                for(var i=0;i<jsonData.length;i++){
                    if(jsonData[i]*1 < 10){
                        level1[jsonData[i]*1-1].checked = true;
                    }
                    if(jsonData[i]*1 > 10){
                        level2[jsonData[i]*1-11].checked = true;
                    }
                }
                that.setData({
                    level1: level1,
                    level2: level2
                });
            }
        });
    },
    //获取总人数
    getTotal: function (type) {
        var that = this;
        util.wxAjax("admin/getCount","post",{
            type: type
        },function(res){
            var data = res.data;
            if(data.result == 200 || data.result == "200"){
                that.setData({
                    total:data.data
                });
            }else{
                wx.showToast({
                    title: data.msg,
                    icon:"none"
                })
            }
        });
    }
})